using Holoville.HOTween;
using UnityEngine;

public class RotationObject : MonoBehaviour
{
	public float intansive = 1f;

	public float animationTime = 2f;

	public Vector3 shiftPosition = new Vector3(0f, 360f, 0f);

	public bool usedAnimation;

	public bool usedDragRotation = true;

	private float _coeffAngle;

	public GameObject rotateObject;

	private Camera _camera;

	private Vector3 _startAngle;

	public Transform pointLeftBot;

	public Transform pointRightTop;

	private Vector3? _prevPosition;

	private bool _click;

	private bool _inArea;

	private bool _stateAnimation;

	public GameObject PanelMainMenu;

	private Quaternion defaultRotation;

	private Tweener rotationTweener;

	private void Start()
	{
		_camera = UICamera.mainCamera;
		if (rotateObject == null)
		{
			rotateObject = base.gameObject;
		}
		_coeffAngle = 360f / Mathf.Abs(pointLeftBot.position.x - pointRightTop.position.x) * intansive;
		_startAngle = rotateObject.transform.localEulerAngles;
		if (animationTime <= 0f)
		{
			animationTime = 2f;
		}
	}

	private void OnEnable()
	{
		defaultRotation = rotateObject.transform.rotation;
		_click = false;
		_stateAnimation = false;
		_prevPosition = null;
		_inArea = false;
	}

	private void OnDisable()
	{
		if (rotateObject != null)
		{
			if (rotationTweener != null)
			{
				HOTween.Kill(rotationTweener);
			}
			rotateObject.transform.rotation = defaultRotation;
		}
	}

	public void ResetRotation()
	{
		base.enabled = false;
		base.enabled = true;
	}

	public void AnimationOnStartPosition()
	{
		rotateObject.transform.localEulerAngles = Vector3.zero;
		OnEnable();
		AutoAnimation();
	}

	private void AutoAnimation()
	{
		float p_duration = animationTime;
		Vector3 vector = rotateObject.transform.localEulerAngles + shiftPosition;
		if (rotationTweener != null)
		{
			HOTween.Kill(rotationTweener);
		}
		rotationTweener = HOTween.To(rotateObject.transform, p_duration, new TweenParms().Prop("localEulerAngles", vector).Ease(EaseType.Linear).UpdateType(UpdateType.TimeScaleIndependentUpdate)
			.Loops(-1));
	}

	private void Update()
	{
		if (_camera == null)
		{
			BCWDebug.Log("camera not found");
		}
		if (!rotateObject.activeSelf)
		{
			return;
		}
		if (usedAnimation && !_stateAnimation)
		{
			_stateAnimation = true;
			AutoAnimation();
		}
		if (!usedDragRotation)
		{
			return;
		}
		Vector3 position = Vector3.zero;
		if (Input.touchCount > 0)
		{
			_stateAnimation = true;
			position = new Vector3(Input.touches[0].position.x, Input.touches[0].position.y, 1f);
			position = _camera.ScreenToWorldPoint(position);
			if (!_click)
			{
				_click = true;
				ClickEvent(position);
			}
		}
		else
		{
			_prevPosition = null;
			_inArea = false;
			if (_click)
			{
				_stateAnimation = false;
			}
			_click = false;
		}
		if (_click && _inArea)
		{
			InputEvent(position);
		}
	}

	private void ClickEvent(Vector3 position)
	{
		if (position.x > pointLeftBot.position.x && position.y > pointLeftBot.position.y && position.x < pointRightTop.position.x && position.y < pointRightTop.position.y)
		{
			_inArea = true;
			_stateAnimation = true;
			if (rotationTweener != null)
			{
				HOTween.Kill(rotationTweener);
			}
		}
	}

	private void InputEvent(Vector3 position)
	{
		if (_inArea)
		{
			Vector3? prevPosition = _prevPosition;
			if (!prevPosition.HasValue)
			{
				_prevPosition = position;
				return;
			}
			Transform obj = rotateObject.transform;
			Vector3 vector = new Vector3(_startAngle.x, rotateObject.transform.localEulerAngles.y, _startAngle.z);
			float x = position.x;
			Vector3? prevPosition2 = _prevPosition;
			obj.localEulerAngles = vector + new Vector3(0f, (0f - (x - prevPosition2.Value.x)) * _coeffAngle, 0f);
			_prevPosition = position;
		}
		else
		{
			_prevPosition = null;
		}
	}
}
